/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiof.moraa.server.dao;

import com.hiof.moraa.mapping.TblAthlete;
import com.hiof.moraa.mapping.TblRoute;
import com.hiof.moraa.mapping.TblSupport;
import com.hiof.moraa.mapping.TblUser;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Bergy
 */
public class HibernateRouteDAOImp implements RouteDAO {

    private SessionFactory factory;

    public SessionFactory getFactory() {
        return factory;
    }

    public void setFactory(SessionFactory factory) {
        this.factory = factory;
    }

    @Override
    public List<TblRoute> getTblRoute(String username) {
        Session session = factory.getCurrentSession();
        List<TblRoute> result = new ArrayList<TblRoute>();

        String hql = "from TblUser user where user.username = :username";
        Query query = session.createQuery(hql);
        query.setString("username", username);
        TblUser user = (TblUser) query.uniqueResult();

        for (Iterator object = user.getTblAthletes().iterator(); object.hasNext();) {
            TblAthlete athlete = (TblAthlete) object.next();
            result.add(athlete.getTblRoute());
        }

        for (Iterator object = user.getTblSupports().iterator(); object.hasNext();) {
            TblSupport support = (TblSupport) object.next();
            result.add(support.getTblRoute());
        }

        return result;
    }

    @Override
    public TblRoute getTblRoute(int routeId) {
        TblRoute route = (TblRoute) factory.getCurrentSession().get(TblRoute.class, routeId);
        return route;
    }
}
